<template>
  <el-row class="row" type="flex" align="middle" justify="center">
    <el-col :span="8" class="col">
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        :rules="rules"
        class="loginForm"
        label-position="top"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script>
// eslint-disable-next-line
export default {
  data () {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      this.$refs.form.validate(async res => {
        if (!res) {
          return
        }
        // eslint-disable-next-line
        let url = "login";
        let response = await this.axios.post(url, this.form)
        if (response.data.meta.status === 200) {
          this.$message({
            message: response.data.meta.msg,
            type: 'success',
            duration: 800
          })
          sessionStorage.setItem('token', response.data.data.token)
          this.$router.push('/home')
        } else {
          this.$message({
            message: response.data.meta.msg,
            type: 'error',
            duration: 800
          })
        }

        // .then(res => {
        //   console.log(res)
        //   if (res.data.meta.status === 200) {
        //     this.$message({
        //       message: res.data.meta.msg,
        //       type: 'success',
        //       duration: 800
        //     })
        //     sessionStorage.setItem('token', res.data.data.token)
        //     this.$router.push('/home')
        //   } else {
        //     this.$message({
        //       message: res.data.meta.msg,
        //       type: 'error',
        //       duration: 800
        //     })
        //   }
        // })
      })
    },
    onReset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="less">
.row {
  min-width: 910px;
  height: 100%;
  background-color: #2d434c;
  .loginForm {
    background-color: #fff;
    padding: 25px;
    border-radius: 15px;
  }
}
</style>
